package org.scut.smartgeometry.act.adapter;


import android.app.Activity;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.scut.smartgeometry.R;
import org.scut.smartgeometry.ent.ViewCache;

import java.util.List;

/**
 * MainMenu的适配器，
 * 按钮的布局，匹配。
 */
public class ImageAndTextAdapter extends ArrayAdapter<String>{
	
	private ListView listView;
	
	public ImageAndTextAdapter(Activity context,
			List<String> btn_names, ListView listView) {
		super(context, 0, btn_names);
		// TODO Auto-generated constructor stub
		this.listView = listView;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Activity activity = (Activity) getContext();
		
		//inflate the views from XML
		View view = convertView;
		ViewCache viewCache;
		
        String btn_name = getItem(position);

//        Log.i("hb", btn_name);

        if(view == null){
			LayoutInflater inflater = activity.getLayoutInflater();
			view = inflater.inflate(R.layout.single_menu, null);
			viewCache = new ViewCache(view);
			view.setTag(viewCache);
		}
		else{
			viewCache = (ViewCache) view.getTag();
		}
		
		
		ImageView imageView = viewCache.getImageView();
        TextView textView = viewCache.getTextView();

        fitImgAndTxt(imageView, textView, btn_name);


		return view;
	}

	

    private void fitImgAndTxt(ImageView imageView, TextView textView, String btn_name){

        Resources resource = getContext().getResources();

        if (btn_name.equalsIgnoreCase(resource.getString(R.string.undo))) {
            imageView.setImageDrawable(resource.getDrawable(R.drawable.btn_undo));
            textView.setText(R.string.undo);
        } else if (btn_name.equalsIgnoreCase(resource.getString(R.string.redo))) {
            imageView.setImageDrawable(resource.getDrawable(R.drawable.btn_redo));
            textView.setText(R.string.redo);
        } else if (btn_name.equalsIgnoreCase(resource.getString(R.string.clear))) {
            imageView.setImageDrawable(resource.getDrawable(R.drawable.btn_clear));
            textView.setText(R.string.clear);
        } else if (btn_name.equalsIgnoreCase(resource.getString(R.string.color))) {
            imageView.setImageDrawable(resource.getDrawable(R.drawable.btn_color));
            textView.setText(R.string.color);
        } else if (btn_name.equalsIgnoreCase(resource.getString(R.string.chart))) {
            imageView.setImageDrawable(resource.getDrawable(R.drawable.btn_chart));
            textView.setText(R.string.chart);
        } else if (btn_name.equalsIgnoreCase(resource.getString(R.string.style))) {
            imageView.setImageDrawable(resource.getDrawable(R.drawable.btn_style));
            textView.setText(R.string.style);
        } else if (btn_name.equalsIgnoreCase(resource.getString(R.string.save))) {
            imageView.setImageDrawable(resource.getDrawable(R.drawable.btn_save));
            textView.setText(R.string.save);
        } else if (btn_name.equalsIgnoreCase(resource.getString(R.string.background))) {
            imageView.setImageDrawable(resource.getDrawable(R.drawable.btn_background));
            textView.setText(R.string.background);
        } else if (btn_name.equalsIgnoreCase(resource.getString(R.string.exit))) {
            imageView.setImageDrawable(resource.getDrawable(R.drawable.btn_exit));
            textView.setText(R.string.exit);
        } else if (btn_name.equalsIgnoreCase(resource.getString(R.string.open))) {
            imageView.setImageDrawable(resource.getDrawable(R.drawable.btn_open));
            textView.setText(R.string.open);
        }

    }
	
}
